﻿Public Class frmSelectStaPCode

    Private IsUpdating As Boolean = False

    Private Sub chk_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles chkStation.ItemCheck, chkParameter.ItemCheck
        If Not IsUpdating Then CType(sender, CheckedListBox).ClearSelected() 'only want to respond to mouse clicks
    End Sub

    ''' <summary>
    ''' Check all or none
    ''' </summary>
    Private Sub mnuChkList_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuChkList.ItemClicked
        Dim chk As CheckedListBox = CType(sender, ContextMenuStrip).SourceControl
        With chk
            For i As Integer = 0 To .Items.Count - 1
                .SetItemChecked(i, e.ClickedItem Is mnuCheckAll)
            Next
        End With
        btnOK.Enabled = chkStation.CheckedItems.Count > 0 And chkParameter.CheckedItems.Count > 0
    End Sub

    Private Sub chk_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles chkStation.SelectedValueChanged, chkParameter.SelectedValueChanged
        btnOK.Enabled = chkStation.CheckedItems.Count > 0 And chkParameter.CheckedItems.Count > 0
    End Sub

    Private Sub btnStaAll_Click(sender As System.Object, e As System.EventArgs) Handles btnStaAll.Click, btnStaNone.Click, btnParmAll.Click, btnParmNone.Click
        Dim chk As CheckedListBox = IIf(sender Is btnStaAll Or sender Is btnStaNone, chkStation, chkParameter)
        chk.SuspendLayout()
        Dim IsAll As Boolean = (sender Is btnStaAll Or sender Is btnParmAll)
        IsUpdating = True
        For i As Integer = 0 To chk.Items.Count - 1
            chk.SetItemChecked(i, IsAll)
        Next
        IsUpdating = False
        btnOK.Enabled = chkStation.CheckedItems.Count > 0 And chkParameter.CheckedItems.Count > 0
        chk.ResumeLayout()
    End Sub

    Private Sub frmSelectStaPCode_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If chkTitle.Checked Then SaveSetting(REGAPP, Name, "TitlePrefix", txtTitle.Text)
        If chkYMin.Checked Then SaveSetting(REGAPP, Name, "YMin", txtYMin.Text)
        If chkYMax.Checked Then SaveSetting(REGAPP, Name, "YMax", txtYMax.Text)
    End Sub

    Private Sub frmSelectStaPCode_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        txtTitle.Text = GetSetting(REGAPP, Name, "TitlePrefix", "Branch ID: ~B")
        txtYMin.Text = GetSetting(REGAPP, Name, "YMin")
        txtYMax.Text = GetSetting(REGAPP, Name, "YMax")
        btnOK.Enabled = chkStation.CheckedItems.Count > 0 And chkParameter.CheckedItems.Count > 0
        If ActiveGraphTab() IsNot Nothing Then
            cboGraphType.SelectedIndex = ActiveGraphTab.GraphType
        Else
            cboGraphType.SelectedIndex = 0
        End If
    End Sub

    Private Sub chkTitle_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkTitle.CheckedChanged
        txtTitle.Enabled = chkTitle.Checked
    End Sub

    Private Sub chkYMin_CheckedChanged(sender As Object, e As EventArgs) Handles chkYMin.CheckedChanged
        txtYMin.Enabled = chkYMin.Checked
    End Sub

    Private Sub chkYMax_CheckedChanged(sender As Object, e As EventArgs) Handles chkYMax.CheckedChanged
        txtYMax.Enabled = chkYMax.Checked
    End Sub

End Class